home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / mouspp / demo.cpp < prev    next >
C/C++ Source or Header  |  1992-10-07  |  13KB  |  460 lines

  1. /* -------------------------------------------------------------------- */
  2. /* Mouse++ Version 4.0             demo.cpp            Revised 10/05/92 */
  3. /*                                                                      */
  4. /* General mouse class for Turbo C++/Borland C++.                       */
  5. /* Copyright 1991, 1992 by Carl W. Moreland                             */
  6. /* -------------------------------------------------------------------- */
  7. /* Demonstration of Mouse++ in text mode and graphics mode.             */
  8. /* -------------------------------------------------------------------- */
  9.  
  10. #include <dos.h>
  11. #include <stdio.h>
  12. #include <conio.h>
  13. #include <stdlib.h>
  14. #include <graphics.h>
  15. #include "mouse.h"
  16. #include "cursor.h"
  17.  
  18. void textdemo(void);
  19. int  textscreen(void);
  20. void alttextscreen(void);
  21. void nexttdemo(char *);
  22. void graphicdemo(void);
  23. int  graphicscreen(void);
  24. void nextgdemo(char *);
  25.  
  26. int maxx, maxy;
  27. int text_width, text_height;
  28. struct text_info t_info;
  29. int done = 0;
  30.  
  31. MouseEventHandler myHandler(MB_PRESSED|MB_RELEASED, MouseHandler);
  32.  
  33. main()
  34. {
  35.   if(mouse.Exists())                // check for mouse
  36.   {
  37.     mouse.InstallHandler(0xFF);        // install event handler
  38.     mouse.Enable();            // enable mouse
  39.     textdemo();                // run text mode demo
  40.  
  41.     mouse.SetRepeatRate(CENTERBUTTON,    // set the center button to repeat
  42.                         500, 200);
  43.     graphicdemo();            // run graphics mode demo
  44.     return(0);                // normal termination
  45.   }
  46.   return(1);                // abnormal termination
  47. }
  48.  
  49. /* ----- text mode demo ----------------------------------------------- */
  50.  
  51. void textdemo()                // main routine for text mode demo
  52. {
  53.   textmode(C80);            // 80x25 color text mode
  54.   if(textscreen() == 0)            // draw the screen
  55.   {
  56.     alttextscreen();            // draw the alt screen
  57.     textcolor(13);
  58.     gotoxy(1,3);
  59.  
  60.     if(mouse.Info.type == 1) cprintf(" Bus mouse found\n");
  61.     if(mouse.Info.type == 2) cprintf(" Serial mouse found\n");
  62.     if(mouse.Info.type == 3) cprintf(" InPort mouse found\n");
  63.     if(mouse.Info.type == 4) cprintf(" PS/2 mouse found\n");
  64.     if(mouse.Info.type == 5) cprintf(" HP mouse found\n");
  65.     gotoxy(1,4);
  66.     cprintf(" Software version is %d.%d\n", (int)mouse.Info.majorvers,
  67.               (int)mouse.Info.minorvers);
  68.  
  69.     mouse.yLimit(0, 199);        // set y limit
  70.     mouse.Show();            // initial turn-on
  71.  
  72.     if(!done) { mouse.SetCursor(tdef); nexttdemo("     Default Cursor      "); }
  73.     if(!done) { mouse.SetCursor(txt1); nexttdemo(" 0000h 0E0Fh Soft Cursor "); }
  74.     if(!done) { mouse.SetCursor(txt2); nexttdemo(" 7000h 0E0Fh Soft Cursor "); }
  75.     if(!done) { mouse.SetCursor(txt3); nexttdemo(" 7F00h FF0Fh Soft Cursor "); }
  76.     mouse.xLimit(16, 623);
  77.     if(!done) { mouse.SetCursor(txt4); nexttdemo(" 00FFh 0FD4h Soft Cursor "); }
  78.  
  79.     mouse.Hide();
  80.     textmode(C4350);            // 43/50 line mode
  81.     _setcursortype(_NOCURSOR);        // turn the hardware cursor off
  82.     mouse.xLimit(0, 639);        // set x limit
  83.     gettextinfo(&t_info);
  84.     if(t_info.screenheight == 43)
  85.       mouse.yLimit(0, 349);        // set y limit to EGA 43 line
  86.     else
  87.       mouse.yLimit(0, 399);        // set y limit to VGA 50 line
  88.     mouse.Show();
  89.     if(!done) { mouse.SetCursor(txt4); nexttdemo("     Default Cursor      "); }
  90.   }
  91. }
  92.  
  93. int textscreen(void)            // draws screen for demo
  94. {
  95.   register int i, j;
  96.   char far *video = (char far *)MK_FP(0xB800, 0x0000);
  97.  
  98.   _setcursortype(_NOCURSOR);        // turn the hardware cursor off
  99.  
  100.   for(i=0; i<50; i++)            // draw the text screen background
  101.   {
  102.     for(j=0; j<80; j++)
  103.     {
  104.       *video++ = 0xDB+2*(j/10);        // write the character to video memory
  105.       *video++ = 16*(j/10)+j/10+1;    // write the color
  106.     }
  107.   }
  108.  
  109.   gotoxy(27,1);
  110.   textattr(4);
  111.   cprintf(" Text Mode Demonstration ");
  112.  
  113.   textattr(LIGHTGRAY);
  114.   for(i=5; i<13; i++)
  115.   {
  116.     gotoxy(5, i);
  117.     cprintf("██████████");
  118.   }
  119.   textattr(DARKGRAY);
  120.   for(i=6; i<8; i++)
  121.   {
  122.     gotoxy(6, i);
  123.     cprintf("██");
  124.     gotoxy(9, i);
  125.     cprintf("██");
  126.     gotoxy(12, i);
  127.     cprintf("██");
  128.   }
  129.   textattr(WHITE);
  130.   gotoxy(6,9);
  131.   cprintf("Position");
  132.  
  133.   textattr(LIGHTBLUE);
  134.   gotoxy(37,22); cprintf("╔══════╗");
  135.   gotoxy(37,23); cprintf("║ Next ║");
  136.   gotoxy(37,24); cprintf("╚══════╝");
  137.  
  138.   return(0);
  139. }
  140.  
  141. void alttextscreen(void)        // copies page0 to page2
  142. {
  143.   register int i, j;
  144.  
  145.   char far *page0 = (char far *)MK_FP(0xB800, 0x0000);
  146.   char far *page2 = (char far *)MK_FP(0xBA00, 0x0000);
  147.  
  148.   for(i=0; i<50; i++)            // draw the text screen background
  149.   {
  150.     for(j=0; j<80; j++)
  151.     {
  152.       *page2++ = *page0++;        // copy the characters
  153.       *page2++ = (*page0++ << 1);    // copy the color*2
  154.     }
  155.   }
  156. }
  157.  
  158. void nexttdemo(char *str)
  159. {
  160.   static int page = 0;
  161.  
  162.   textattr(4);
  163.   gotoxy(27,2);
  164.   cprintf(str);                // print title
  165.   textattr(13);
  166.   for(;;)
  167.   {
  168.     mouse.GetEvent();
  169.     textattr(WHITE);
  170.     gotoxy(6,10);
  171.     cprintf(" %2d  %2d ", mouse.xPos() >> 3, mouse.yPos() >> 3);
  172.  
  173.     (mouse.LB_Dn()) ? textattr(RED) :
  174.                       textattr(DARKGRAY);
  175.     gotoxy(6,6);
  176.     cprintf("██");
  177.     gotoxy(6,7);
  178.     cprintf("██");
  179.     (mouse.CB_Dn()) ? textattr(RED) :
  180.                       textattr(DARKGRAY);
  181.     gotoxy(9,6);
  182.     cprintf("██");
  183.     gotoxy(9,7);
  184.     cprintf("██");
  185.  
  186.     (mouse.RB_Dn()) ? textattr(RED) :
  187.                       textattr(DARKGRAY);
  188.     gotoxy(12,6);
  189.     cprintf("██");
  190.     gotoxy(12,7);
  191.     cprintf("██");
  192.  
  193.     if(mouse.Released(LEFTBUTTON))    // check for LB release
  194.       if(mouse.InBox(36*8, 21*8, 43*8, 23*8))
  195.         return;
  196.  
  197.     if(mouse.DoubleClick(RIGHTBUTTON))
  198.     {
  199.       mouse.ClearClick(RIGHTBUTTON);
  200.       return;
  201.     }
  202.  
  203.     if(mouse.DoubleClick(CENTERBUTTON))
  204.     {
  205.       mouse.Disable();
  206.       textattr(4);
  207.       gotoxy(1,1);
  208.       cprintf("Hit any key...");
  209.       while(!getch());
  210.       textattr(DARKGRAY);
  211.       gotoxy(1,1);
  212.       cprintf("██████████████");
  213.       mouse.Enable();
  214.       mouse.Show();
  215.     }
  216.  
  217.     if(mouse.Pressed(LEFTBUTTON) && (mouse.Button() & CTRL_PRESSED))
  218.     {
  219.       done = 1;
  220.       return;
  221.     }
  222.  
  223.     if(mouse.Pressed(LEFTBUTTON) && (mouse.Button() & SHIFT_PRESSED))
  224.     {
  225.       mouse.Hide();
  226.       page = (page == 0 ? 2 : 0);
  227.  
  228.       _AH = 0x05;
  229.       _AL = page;
  230.       geninterrupt(0x10);
  231.  
  232.       mouse.SetVideoPage(page);
  233.       mouse.Show();
  234.     }
  235.   }
  236. }
  237.  
  238. /* ----- graphics mode demo ------------------------------------------- */
  239.  
  240. void graphicdemo()            // main routine for graphics demo
  241. {
  242.   mouse.Hide();
  243.   if(graphicscreen() == 0)        // draw the screen
  244.   {
  245.     done = 0;
  246.     mouse.yLimit(0, maxy);        // set y limit (EGA=350, VGA=480)
  247.     mouse.Move(maxx/2, maxy/2);
  248.     mouse.Show();            // initial turn on
  249.  
  250.     mouse.SetSpeedThreshold(32);
  251.     mouse.MickToPix(8, 8);
  252.     if(!done) { mouse.SetCursor(gcDefault);   nextgdemo(" Default "); }
  253.     if(!done) { mouse.SetCursor(gcCross);     nextgdemo("  Cross  "); }
  254.     if(!done) { mouse.SetCursor(gcCheck);     nextgdemo("  Check  "); }
  255.     if(!done) { mouse.SetCursor(gcPlus);      nextgdemo("  Plus   "); }
  256.     if(!done) { mouse.SetCursor(gcGun);       nextgdemo("   Gun   "); }
  257.     if(!done) { mouse.SetCursor(gcHand);      nextgdemo("  Hand   "); }
  258.     if(!done) { mouse.SetCursor(gcHourglass); nextgdemo("Hourglass"); }
  259.     if(!done) { mouse.SetCursor(gcBullseye);  nextgdemo(" Bullseye"); }
  260.  
  261.     mouse.MickToPix(4, 4);
  262.     if(!done) { mouse.SetCursor(gcIbeam);     nextgdemo("  IBeam  "); }
  263.  
  264.     mouse.MickToPix(2, 2);
  265.     if(!done) { mouse.SetCursor(gcJet);       nextgdemo("   Jet   "); }
  266.  
  267.     mouse.MickToPix(16, 16);
  268.     mouse.Move(maxx/2, maxy/2);
  269.     if(!done) { mouse.SetCursor(gcFace);      nextgdemo("  Face   "); }
  270.  
  271.     if(!done) { mouse.SetCursor(cgcMousepp);  nextgdemo("cMousepp "); }
  272.  
  273.     mouse.InstallHandler(myHandler);    // turn off the motion counter
  274.  
  275.     if(!done) { mouse.SetCursor(gcDefault);   nextgdemo(" Default "); }
  276.     closegraph();
  277.   }
  278. }
  279.  
  280. int graphicscreen()
  281. {
  282.   int x, y;
  283.   int driver = EGA, mode = EGAHI;
  284.  
  285.   initgraph(&driver, &mode, "");
  286.   if(graphresult() == grOk)
  287.   {
  288.     maxx = getmaxx();
  289.     maxy = getmaxy();
  290.     text_width  = textwidth("O");
  291.     text_height = textheight("O");
  292.     setviewport(0, 0, maxx, maxy, 1);
  293.  
  294.     setcolor(RED);            // paint background
  295.     rectangle(0, 0, maxx/3, maxy);
  296.     setfillstyle(SOLID_FILL, RED);
  297.     floodfill(maxx/6, maxy/2, RED);
  298.     setcolor(GREEN);
  299.     rectangle(maxx/3, 0, 2*maxx/3, maxy);
  300.     setfillstyle(SOLID_FILL, GREEN);
  301.     floodfill(maxx/2, maxy/2, GREEN);
  302.     setcolor(BLUE);
  303.     rectangle(2*maxx/3, 0, maxx, maxy);
  304.     setfillstyle(SOLID_FILL, BLUE);
  305.     floodfill(5*maxx/6, maxy/2, BLUE);
  306.     x = (maxx/2)-75;            // center white block
  307.     y = (maxy/2)-75;
  308.     setcolor(WHITE);
  309.     rectangle(x, y, x+150, y+150);
  310.     setfillstyle(SOLID_FILL, WHITE);
  311.     floodfill(x+10, y+10, WHITE);
  312.  
  313.     x = (maxx/2)-40;                    // center black box
  314.     y = (maxy/2)-40;
  315.     setcolor(BLACK);
  316.     rectangle(x, y, x+80, y+80);
  317.     setfillstyle(SOLID_FILL, BLACK);
  318.     floodfill(maxx/2, maxy/2, BLACK);
  319.  
  320.     setcolor(BLACK);            // "Next" box
  321.     moveto(37*text_width, 40*text_height); outtext("████████");
  322.     moveto(37*text_width, 41*text_height); outtext("████████");
  323.     moveto(37*text_width, 42*text_height); outtext("████████");
  324.     setcolor(WHITE);
  325.     moveto(37*text_width, 40*text_height); outtext("╔══════╗");
  326.     moveto(37*text_width, 41*text_height); outtext("║ Next ║");
  327.     moveto(37*text_width, 42*text_height); outtext("╚══════╝");
  328.  
  329.     setfillstyle(SOLID_FILL, LIGHTGRAY);// draw mouse and buttons
  330.     setcolor(LIGHTGRAY);
  331.     rectangle(40, 40, 140, 140);
  332.     floodfill(90, 90, LIGHTGRAY);
  333.     setfillstyle(SOLID_FILL, DARKGRAY);
  334.     setcolor(DARKGRAY);
  335.     rectangle(50, 60, 70, 80);
  336.     floodfill(60, 70, DARKGRAY);
  337.     rectangle(80, 60, 100, 80);
  338.     floodfill(90, 70, DARKGRAY);
  339.     rectangle(110, 60, 130, 80);
  340.     floodfill(120, 70, DARKGRAY);
  341.     setcolor(BLACK);
  342.     moveto(60, 100);
  343.     outtext("Position");
  344.  
  345.     return(0);
  346.   }
  347.   return(1);
  348. }
  349.  
  350. void nextgdemo(char *str)
  351. {
  352.   char mpos[10];
  353.   int color;
  354.  
  355.   setcolor(BLACK);            // print label
  356.   moveto((maxx/2-5*text_width),text_height);
  357.   outtext("█████████");
  358.   setcolor(WHITE);
  359.   moveto((maxx/2-5*text_width),text_height);
  360.   outtext(str);
  361.  
  362.   mouse.ClearBuffer();
  363.   mouse.ClearEvent();
  364.  
  365.   for(;;)
  366.   {
  367.     mouse.GetEvent();
  368.                     // set exclusion area
  369.     mouse.Exclude(40, 40, 140, 140);
  370.                                   // see if mouse has moved
  371.     if(mouse.xCount() || mouse.yCount())
  372.     {
  373.       sprintf(mpos, "%3d %3d", mouse.xPos(), mouse.yPos());
  374.       setcolor(LIGHTGRAY);
  375.       moveto(60, 108);
  376.       outtext("███████");
  377.       moveto(60, 108);
  378.       outtext("███████");
  379.       setcolor(BLACK);
  380.       moveto(60, 108);
  381.       outtext(mpos);            // print cursor position
  382.     }
  383.                     // check all button status for change
  384.     if(mouse.Pressed(LEFTBUTTON) || mouse.Released(LEFTBUTTON))
  385.     {
  386.       color = mouse.LB_Dn() ? RED : DARKGRAY;
  387.       if(mouse.MultiClick(LEFTBUTTON) == 2) color = BLUE;
  388.       if(mouse.MultiClick(LEFTBUTTON) == 3) color = GREEN;
  389.       if(mouse.MultiClick(LEFTBUTTON) >= 4) color = YELLOW;
  390.  
  391.       setcolor(color);
  392.       setfillstyle(SOLID_FILL, color);
  393.       rectangle(50, 60, 70, 80);
  394.       floodfill(60, 70, color);
  395.     }
  396.     if(mouse.Pressed(CENTERBUTTON) || mouse.Released(CENTERBUTTON))
  397.     {
  398.       color = mouse.CB_Dn() ? RED : DARKGRAY;
  399.       if(mouse.MultiClick(CENTERBUTTON) == 2)
  400.       {
  401.         if(mouse.CB_Dn())
  402.           color = BLUE;
  403.       }
  404.       if(mouse.MultiClick(CENTERBUTTON) == 3)
  405.       {
  406.         if(mouse.CB_Dn())
  407.           color = GREEN;
  408.       }
  409.       if(mouse.MultiClick(CENTERBUTTON) == 4)
  410.       {
  411.         color = YELLOW;
  412.         mouse.ClearClick(CENTERBUTTON);
  413.       }
  414.  
  415.       setcolor(color);
  416.       setfillstyle(SOLID_FILL, color);
  417.       rectangle(80, 60, 100, 80);
  418.       floodfill(90, 70, color);
  419.     }
  420.     if(mouse.Pressed(RIGHTBUTTON) || mouse.Released(RIGHTBUTTON))
  421.     {
  422.       color = mouse.RB_Dn() ? RED : DARKGRAY;
  423.       if(mouse.MultiClick(RIGHTBUTTON) == 2) color = BLUE;
  424.       if(mouse.MultiClick(RIGHTBUTTON) == 3) color = GREEN;
  425.       if(mouse.MultiClick(RIGHTBUTTON) >= 4) color = YELLOW;
  426.  
  427.       setcolor(color);
  428.       setfillstyle(SOLID_FILL, color);
  429.       rectangle(110, 60, 130, 80);
  430.       floodfill(120, 70, color);
  431.     }
  432.  
  433.     if(mouse.Released(LEFTBUTTON))    // check for LB release
  434.     {
  435.       if(mouse.InBox(37*text_width, 40*text_height,
  436.                    45*text_width, 43*text_height))
  437.       {
  438.         setcolor(DARKGRAY);        // dehilite mouse button
  439.         setfillstyle(SOLID_FILL, DARKGRAY);
  440.         rectangle(50, 60, 70, 80);
  441.         floodfill(60, 70, DARKGRAY);
  442.  
  443.         return;
  444.       }
  445.     }
  446.  
  447.     if(mouse.MultiClick(RIGHTBUTTON) == 3)
  448.     {
  449.       mouse.ClearClick(RIGHTBUTTON);
  450.       return;
  451.     }
  452.  
  453.     if(mouse.Pressed(LEFTBUTTON) && (mouse.Button() & CTRL_PRESSED))
  454.     {
  455.       done = 1;
  456.       return;
  457.     }
  458.   }
  459. }
  460.